package com.wocai.jrt.orgadmin.stats.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vteba.web.action.JsonBean;
import com.wocai.jrt.orgadmin.appointment.service.spi.AppointmentService;
import com.wocai.jrt.orgadmin.employee.model.Employee;
import com.wocai.jrt.orgadmin.employee.model.EmployeeGroup;
import com.wocai.jrt.orgadmin.employee.service.spi.EmployeeGroupService;
import com.wocai.jrt.orgadmin.employee.service.spi.EmployeeService;
import com.wocai.jrt.orgadmin.investor.model.Investor;
import com.wocai.jrt.orgadmin.investor.service.spi.InvestorService;
import com.wocai.jrt.orgadmin.orders.model.Orders;
import com.wocai.jrt.orgadmin.orders.service.spi.OrdersService;
import com.wocai.jrt.orgadmin.product.model.Product;
import com.wocai.jrt.orgadmin.product.service.spi.ProductService;
import com.wocai.jrt.orgadmin.stats.bean.EmployeeDataSum;
import com.wocai.jrt.orgadmin.stats.bean.IndexStatsReqBean;
import com.wocai.jrt.orgadmin.stats.bean.Pie;
import com.wocai.jrt.orgadmin.utils.UserUtils;

/**
 * 投资人统计控制器
 * 
 * @author linbaolu
 * @date 2015年9月24日 16:33:40
 */
@Controller
@RequestMapping("/platSum")
public class PlatSumAction {
	private static final Logger LOGGER = LoggerFactory.getLogger(PlatSumAction.class);
	@Inject
	private EmployeeService employeeServiceImpl;
	@Inject
	private InvestorService investorServiceImpl;
	@Inject
	private AppointmentService appointmentServiceImpl;
	@Inject
	private ProductService productServiceImpl;
	@Inject
	private OrdersService ordersServiceImpl;
	@Inject
	private EmployeeGroupService employeeGroupServiceImpl;

	/**
	 * 跳转至数据统计首页
	 * 
	 * @param model
	 *            参数
	 * @return 首页路径 String
	 */
	@RequestMapping("/dataSum")
	public String dataSum(Map<String, Object> maps) {
		try {
			// 投资人数
			Investor investorParam = new Investor();
			investorParam.setOrgId(UserUtils.getUser().getOrgId());
			int investorCount = investorServiceImpl.countBy(investorParam);
			maps.put("investorCount", investorCount);

			// 成交投资人数
			IndexStatsReqBean indexStatsReqBean = new IndexStatsReqBean();
			indexStatsReqBean.setOrgId(UserUtils.getUser().getOrgId());
			int investorTurnCount = investorServiceImpl.turnCount(indexStatsReqBean);
			maps.put("investorTurnCount", investorTurnCount);

			// 客户转化率
			maps.put("investorConversion",
					Math.round(((double) investorTurnCount) / investorCount * 100) + "%");

			// 预约数
			int appointmentCount = appointmentServiceImpl.yestodayCount(indexStatsReqBean);
			maps.put("appointmentCount", appointmentCount);

			// 总订单数
			Orders orderParam = new Orders();
			orderParam.setOrgId(UserUtils.getUser().getOrgId());
			orderParam.setDeleted(false);
			int orderCount = ordersServiceImpl.countBy(orderParam);
			maps.put("orderCount", orderCount);

			Orders turnOrderParam = new Orders();
			turnOrderParam.setOrgId(UserUtils.getUser().getOrgId());
			turnOrderParam.setDeleted(false);
			turnOrderParam.setState(6);
			int turnOrderCount = ordersServiceImpl.countBy(turnOrderParam);
			maps.put("turnOrderCount", turnOrderCount);

			// 订单转化率
			maps.put("orderConversion", Math.round(((double) turnOrderCount) / orderCount * 100)
					+ "%");

			// 销售额
			double saleAmount = ordersServiceImpl.saleAmount(indexStatsReqBean);
			maps.put("saleAmount", Math.round(saleAmount / 10000) + "万");

			// 产品数量
			Product productParam = new Product();
			productParam.setOrgId(UserUtils.getUser().getOrgId());
			int productCount = productServiceImpl.countBy(productParam);
			maps.put("productCount", productCount);

			// 在售产品数量
			Product saleProductParam = new Product();
			saleProductParam.setOrgId(UserUtils.getUser().getOrgId());
			saleProductParam.setState(1);
			saleProductParam.setAuditState(1);
			int saleProductCount = productServiceImpl.countBy(saleProductParam);
			maps.put("saleProductCount", saleProductCount);

			// 募集完成产品数
			Product completeProductParam = new Product();
			completeProductParam.setOrgId(UserUtils.getUser().getOrgId());
			completeProductParam.setState(2);
			completeProductParam.setAuditState(1);
			int completeProductCount = productServiceImpl.countBy(completeProductParam);
			maps.put("completeProductCount", completeProductCount);

			// 理财师数
			Employee employeeParam = new Employee();
			employeeParam.setState(1);
			employeeParam.setOrgId(UserUtils.getUser().getOrgId());
			employeeParam.setPlanner(true);
			int employeeCount = employeeServiceImpl.countBy(employeeParam);
			maps.put("employeeCount", employeeCount);

			// 团队数
			EmployeeGroup rgParam = new EmployeeGroup();
			rgParam.setOrgId(UserUtils.getUser().getOrgId());
			rgParam.setDeleted(false);
			rgParam.setPlannerGroup(true);
			int employeeGroupCount = employeeGroupServiceImpl.countBy(rgParam);
			maps.put("employeeGroupCount", employeeGroupCount);

		} catch (Exception e) {
			LOGGER.error("show stats index page error, msg = [{}]", e.getMessage());
		}

		return "stats/index/dataSum";
	}

	/**
	 * 跳转至数据统计首页
	 * 
	 * @param model
	 *            参数
	 * @return 首页路径 String
	 */
	@RequestMapping("/employeeDataSum")
	public String employeeDataSum(Map<String, Object> maps) {
		try {
			IndexStatsReqBean indexStatsReqBean = new IndexStatsReqBean();
			// indexStatsReqBean.setDate(MyDateUtils.getYestoday());
			indexStatsReqBean.setOrgId(UserUtils.getUser().getOrgId());

			List<EmployeeDataSum> employeeDataSum = employeeServiceImpl
					.employeeDataSum(indexStatsReqBean);

			maps.put("employeeDataSum", employeeDataSum);
		} catch (Exception e) {
			LOGGER.error("show employeeDataSum page error, msg = [{}]", e.getMessage());
		}

		return "stats/index/employeeDataSum";
	}

	/**
	 * 跳转至数据统计首页
	 * 
	 * @param model
	 *            参数
	 * @return 首页路径 String
	 */
	@RequestMapping("/groupDataSum")
	public String groupDataSum(Map<String, Object> maps) {
		return "stats/index/groupDataSum";
	}

	/**
	 * 跳转至数据统计首页
	 * 
	 * @param model
	 *            参数
	 * @return 首页路径 String
	 */
	@ResponseBody
	@RequestMapping("/queryGroupDataSum")
	public JsonBean queryGroupDataSum() {
		JsonBean jsonBean = new JsonBean();

		try {
			IndexStatsReqBean indexStatsReqBean = new IndexStatsReqBean();
			// indexStatsReqBean.setDate(MyDateUtils.getYestoday());
			indexStatsReqBean.setOrgId(UserUtils.getUser().getOrgId());

			List<EmployeeDataSum> groupDataSum = employeeServiceImpl
					.groupDataSum(indexStatsReqBean);
			List<String> groupNames = new ArrayList<String>();
			List<Long> groupValues = new ArrayList<Long>();

			if (null != groupDataSum) {
				for (EmployeeDataSum gds : groupDataSum) {
					groupNames.add(gds.getName());
					groupValues.add(Math.round(gds.getSaleAmount() / 10000));
				}
			}

			Map<String, Object> maps = new HashMap<String, Object>();
			maps.put("groupNames", groupNames);
			maps.put("groupValues", groupValues);

			jsonBean.setCode(1);
			jsonBean.setData(maps);
		} catch (Exception e) {
			jsonBean.setCode(0);
			LOGGER.error("show queryGroupDataSum page error, msg = [{}]", e.getMessage());
		}

		return jsonBean;
	}

	@ResponseBody
	@RequestMapping("/queryProductSale")
	public JsonBean queryProductSale() {
		JsonBean jsonBean = new JsonBean();

		try {
			Map<String, Object> maps = new HashMap<String, Object>();
			IndexStatsReqBean indexStatsReqBean = new IndexStatsReqBean();
			indexStatsReqBean.setOrgId(UserUtils.getUser().getOrgId());
			List<Pie> list = productServiceImpl.productSale(indexStatsReqBean);
			List<Pie> serial = new ArrayList<Pie>();
			List<String> names = new ArrayList<String>();
			int other = 0;

			if (null != list) {
				for (int i = 0; i < list.size(); i++) {
					Pie pie = list.get(i);
					pie.setValue(pie.getValue() / 10000);
					if (i <= 9) {
						serial.add(pie);
						names.add(pie.getName());
					} else {
						other += pie.getValue();
					}
				}

				if (list.size() > 10) {
					Pie pie = new Pie();
					pie.setName("其他");
					pie.setValue(other);
					serial.add(pie);
					names.add("其他");
				}
			}

			maps.put("names", names);
			maps.put("serial", serial);
			jsonBean.setCode(1);
			jsonBean.setData(maps);
		} catch (Exception e) {
			jsonBean.setCode(0);
			LOGGER.error("queryProductSale  data error,msg=[{}] " + e.getMessage());
		}

		return jsonBean;
	}

	// /**
	// * 测试缓存
	// */
	// @RequestMapping("/cache")
	// public void testCache() {
	// // 计算投资人统计数据
	// investorStatsServiceImpl.statsAllOrgInvestorInfo();
	// }

}
